//
//  XHStatusToolBar.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/29.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit

class XHStatusToolBar: UIView {

    var buttonRetweet :UIButton?
    var buttonComment :UIButton?
    var buttonUnlike :UIButton?
    
    var status :XHStatus?{
        didSet{
            
        buttonRetweet?.setTitle(status?.reposts_countStr, forState: UIControlState.Normal)
        buttonComment?.setTitle(status?.comments_countStr, forState: UIControlState.Normal)
        buttonUnlike?.setTitle(status?.attitudes_countStr, forState: UIControlState.Normal)
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //设置ui
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        
         buttonRetweet = addChildButtons("timeline_icon_retweet", title: "转发")
         buttonComment = addChildButtons("timeline_icon_comment", title: "评论")
         buttonUnlike = addChildButtons("timeline_icon_unlike", title: "赞")
        //添加线
        let lineLeft = addChildLines()
        let lineRight = addChildLines()
        
        
        buttonRetweet?.snp_makeConstraints { (make) -> Void in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(buttonComment!)
        }
        buttonComment?.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.left.equalTo(buttonRetweet!.snp_right)
            make.width.equalTo(buttonUnlike!)
        }
        buttonUnlike?.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.right.equalTo(self)
            make.left.equalTo(buttonComment!.snp_right)
        }
        //添加线的约束
        lineLeft.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(buttonRetweet!.snp_right)
            make.centerY.equalTo(self)
        }
        lineRight.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(buttonUnlike!.snp_left)
        }
    }
    
    private func addChildButtons(imgStr:String,title:String)->UIButton{
        let button = UIButton()
        button.setImage(UIImage(named: imgStr), forState: UIControlState.Normal)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        //添加button
        addSubview(button)
        
        return button
    }
    
    //添加分割线
    private func addChildLines() ->UIImageView{
        let imgView = UIImageView(imgName: "timeline_card_bottom_line")
        addSubview(imgView)
        return imgView
    }
    
}
